﻿using EquipSecs300;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EquipSecs300
{
    public partial class FormPassword : Form
    {
        DateTime _dtStart;
        public event Action<int>  PassWordLogIn;
        bool trig;
        public FormPassword()
        {
            InitializeComponent();

        }

        public void ShowForm()
        {
            tbxPW.Text = "";
            lblNotice.Text = "";
            this.ShowDialog();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            PassWordConfirm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            tbxPW.Text = "";
            lblNotice.Text = "";
            this.Visible = false;
        }

        private void tbxPW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PassWordConfirm();
            }
        }

        private void PassWordConfirm()
        {
            string str = tbxPW.Text.Trim();
            if (DateTime.Now.ToString("MMdd") == str)
            {
                PassWordLogin();
                _dtStart = DateTime.Now;
                tbxPW.Text = "";
                lblNotice.Text = "";
                this.Visible = false;
            }
            else
            {
                PassWordLogout();
                lblNotice.Text = "密码错误";
            }
        }
        public void PassWordLogin()
        {
            GlobalVar.IsLogIn = 1;
            trig = false;
            PassWordLogIn?.Invoke(1);
        }
        public void PassWordLogout()
        {
            GlobalVar.IsLogIn = 0;
            PassWordLogIn?.Invoke(0);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Subtract(_dtStart).TotalMinutes >= 30 && !trig && false)
            {
                PassWordLogout();
                trig = true;
            }
        }

        private void tbxPW_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            tbxPW.Text = DateTime.Now.ToString("MMdd");
        }
    }
}
